<?php

namespace App\Containers\AppSection\User\Actions;

use App\Containers\AppSection\Confirmation\Tasks\FindConfirmationByCodeTask;
use App\Containers\AppSection\Log\Tasks\CreateLogTask;
use App\Ship\Exceptions\InternalErrorException;
use App\Ship\Parents\Actions\Action;
use App\Ship\Parents\Exceptions\Exception;
use App\Ship\Parents\Requests\Request;
use Illuminate\Support\Facades\Auth;

class ChangeEmailAction extends Action
{
    public function run(Request $request): void
    {
        //Проверка кода почты
        app(FindConfirmationByCodeTask::class)
            ->run($request->email_code, $request->email, config('appSection-confirmation.confirms.email.requests.change'), config('appSection-confirmation.confirms.email.type'));

        $user = Auth::user();

        try {
            $user->forceFill([
                'email' => $request->email,
            ])->save();
        } catch (Exception $e) {
            throw new InternalErrorException();
        }
        app(CreateLogTask::class)->run($request->profile()->id, 'Смена почты', 'user');
    }
}
